#region Copyright
/*
Copyright (c) 2008 EPiServer AB.  All rights reserved.

This code is released by EPiServer AB under the Source Code File - Specific License Conditions, published August 20, 2007. 
See http://www.episerver.com/Specific_License_Conditions for details. 
*/
#endregion

#region Known Limitations
/*
The template has no known limitations.
*/
#endregion
using EPiServer.Templates.RelatePlus;
using System;
using EPiServer.Common;
using EPiServer.Common.Tags;

namespace EPiServer.Research.Wiki.Units
{
    /// <summary>
    /// UserControl to render TagCloud
    /// </summary>
    public partial class Tags : EPiServer.Research.Wiki.DynamicContent.WikiElement
    {

        protected override void OnLoad(EventArgs e)
        {

            TagCloud = TagHandler.GetTagCloud();
            this.DataBind();
            
            base.OnLoad(e);
        }
        #region Properties
        /// <summary>
        /// The Tag Cloud to render
        /// </summary>
        public TagCloud TagCloud
        {
            get;
            set;
        }
        #endregion

        #region Method overrides
        /// <summary>
        /// Binds a data source to the invoked server control and all its child controls.
        /// </summary>
        public override void DataBind()
        {
            if (TagCloud != null)
            {
                

                lvTags.DataSource = TagCloud.Items;
                lvTags.DataBind();
            }
        }
        #endregion

        #region Protected methods
        /// <summary>
        /// Creates the Tag link
        /// </summary>
        /// <param name="item"></param>
        /// <returns>The tagcloud html</returns>        
        protected string RenderTag(TagCloudItem item)
        {
            return String.Format("<h{0}><a rel=\"rel\" href=\"{1}\" title=\"{2} {3}\">{4}</a></h{0}>",
                GetTagWeight(item), item.Tag.GetTagsUri(),
                item.Tag.GetNumberOfTaggedItems((Site)null), Translate("/general/numberoftaggeditems"),
                item.Tag.Name.FormatContentText());
        }
        #endregion

        #region Private methods
        /// <summary>
        /// Determines the tag weight to be used
        /// </summary>
        /// <param name="tagItem">The tag item to determine weight of</param>
        /// <returns>The tag weight</returns>
        private int GetTagWeight(TagCloudItem tagItem)
        {
            int tagWeight;
            switch (tagItem.Weight)
            {
                case 1:
                case 2:
                    tagWeight = 6;
                    break;
                case 3:
                case 4:
                    tagWeight = 5;
                    break;
                case 5:
                case 6:
                    tagWeight = 4;
                    break;
                case 7:
                case 8:
                    tagWeight = 3;
                    break;
                case 9:
                    tagWeight = 2;
                    break;
                case 10:
                    tagWeight = 1;
                    break;
                default:
                    tagWeight = 6;
                    break;
            }

            return tagWeight;
        }
        #endregion

        protected override string GetAscxUrl()
        {
            return "~/Templates/WikiX/Units/Tags.ascx";
        }
    }

    
}